/**
 * 
 */
package com.uit.trip.utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

/**
 * @author User
 * 
 */
public class FileCommon {

	/*
	 * Save image to sdcard.
	 */
	public static String getOutputFile() {
		// To be safe, you should check that the SDCard is mounted
		// using Environment.getExternalStorageState() before doing this.
		File mediaStorageDir = new File(
				Environment.getExternalStorageDirectory(), "TripTracker");
		// This location works best if you want the created images to be shared
		// between applications and persist after your app has been uninstalled.

		// Create the storage directory if it does not exist
		if (!mediaStorageDir.exists()) {
			if (!mediaStorageDir.mkdirs()) {
				return null;
			}
		}

		// Create a media file name
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
				.format(new Date());
		String iPath = mediaStorageDir.getPath() + File.separator + "IMG_"
				+ timeStamp + ".png";

		return iPath;
	}

	/*
	 * Get Uri from path.
	 */
	public static Uri getOutputMediaFileUri(String pPath) {
		return Uri.fromFile(new File(pPath));
	}

	public static String saveImageFile(Context context, Bitmap resultBitmap) {
		String mPath = FileCommon.getOutputFile();
		if (resultBitmap != null && !mPath.equalsIgnoreCase("")) {
			// deleteFile(new File(mPath));
			ProcessImage.getInstance(context).writeBitmapToFile(resultBitmap,
					mPath, "png");
			return mPath;
		}
		return null;
	}
}
